package com.mediacloud.app.newsmodule.fragment

import android.graphics.Color
import android.view.View
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.widget.AppCompatImageView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView
import com.eventTrack.TXReportManager
import com.mediacloud.app.assembly.util.getArticleLogoUrl
import com.mediacloud.app.assembly.util.loadX
import com.mediacloud.app.model.interfaces.DataInvokeCallBack
import com.mediacloud.app.model.news.ArticleItem
import com.mediacloud.app.model.news.CatalogItem
import com.mediacloud.app.model.news.kt.TypeX
import com.mediacloud.app.newsmodule.R
import com.mediacloud.app.newsmodule.adaptor.component.IEvent
import com.mediacloud.app.newsmodule.addnewslike.m.AddLikeDataInvoke
import com.mediacloud.app.newsmodule.fragment.video.x.IVideoHolder
import com.mediacloud.app.newsmodule.utils.CollectionController
import com.mediacloud.app.newsmodule.utils.LoginUtils
import com.mediacloud.app.newsmodule.utils.NewsItemClickUtils
import com.mediacloud.app.newsmodule.utils.share.ShareWrap
import com.mediacloud.app.newsmodule.utils.support.SupportKit
import com.mediacloud.app.reslib.config.NewsDetailStyleConfig
import com.mediacloud.app.reslib.enums.AppFactoryGlobalConfig
import com.mediacloud.app.user.model.UserInfo
import com.mediacloud.app.util.ClickUtils
import com.tencent.mmkv.MMKV

class BigVideoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),
    IVideoHolder,IEvent {
    var shareWrap: ShareWrap? = null
    val titleTv: TextView = itemView.findViewById(R.id.titleTv)
    val logoIv: ImageView = itemView.findViewById(R.id.logoIv)
    val videoTag: View = itemView.findViewById(R.id.videoTag)
    val listSpider: View = itemView.findViewById(R.id.listSpider)
    val spiderAvatar: ImageView = itemView.findViewById(R.id.spiderAvatar)
    val spiderName: TextView = itemView.findViewById(R.id.spiderName)
    val spiderAttention: TextView = itemView.findViewById(R.id.spiderAttention)
    val tv_share_count: TextView = itemView.findViewById(R.id.tv_share_count)
    var iv_share: AppCompatImageView = itemView.findViewById(R.id.iv_share)
    var iv_comment: AppCompatImageView = itemView.findViewById(R.id.iv_comment)
    var iv_collection: AppCompatImageView = itemView.findViewById(R.id.iv_collection)
    var split_line: View = itemView.findViewById(R.id.split_line)
    val durationTv: TextView = itemView.findViewById(R.id.durationTv)
    val outer_view: ConstraintLayout = itemView.findViewById(R.id.outer_view)
    var item: ArticleItem? = null
    var logoContainer: FrameLayout = itemView.findViewById(R.id.logoContainer)
    var logoLayout: FrameLayout = itemView.findViewById(R.id.logoLayout)
    var tvResourceTime: TextView = itemView.findViewById(R.id.tvResourceTime)
    var disableNotifyEvent:Boolean = false

    fun setInfo(item: ArticleItem,pos:Int = 0) {
        this.item = item

        titleTv.text = item.title
        if (MMKV.defaultMMKV().getBoolean("${item.getId()}", false)) {
            titleTv.setTextColor(Color.parseColor("#666666"))
        } else {
            titleTv.setTextColor(Color.parseColor("#222222"))
        }
        logoIv.loadX(getArticleLogoUrl(16,9,item), 3, 1, true, null)
        when (item.type) {
            TypeX.NEWS.VIDEO -> {
                videoTag.visibility = View.VISIBLE
                durationTv.visibility = View.VISIBLE
                durationTv.text = item.prop4

            }
            TypeX.NEWS.LIVE_VIDEO -> {
                videoTag.visibility = View.VISIBLE
                durationTv.visibility = View.GONE
            }
            else -> {
                videoTag.visibility = View.GONE
                durationTv.visibility = View.GONE
            }
        }

       /* if (item.spider != null) {
            spiderAttention.isSelected = item.isFollowed
            if(item.isFollowed){
                spiderAttention.text ="已关注"
            }else{
                spiderAttention.text ="关注"
            }

            spiderAvatar.loadX(item?.spider?.userImage?:item.avatar, 0, false, ContextCompat.getDrawable(itemView.context, R.drawable.ic_launcher))
            spiderName.text = item.spider?.userName
            spiderAttention.setOnClickListener {
                ClickUtils.delayClickable(it)
                if(item.isFollowed){
                    MediaAuthorDetailActivity.start(spiderAvatar.context, item.spider.userId)
                    return@setOnClickListener
                }
                FollowKit.followOrNot(null,
                    itemView.context,
                    item.spider.userId,
                    item.spider.userNickName,
                    null,
                    item,
                    spiderAttention
                )
            }
            spiderAttention.visibility = View.VISIBLE
            split_line.visibility = View.VISIBLE
            spiderAvatar.setOnClickListener { v: View? ->
                ClickUtils.delayClickable(v)
                MediaAuthorDetailActivity.start(
                    spiderAvatar.context,
                    item.spider.userId
                )
            }
            spiderName.setOnClickListener { v: View? ->
                ClickUtils.delayClickable(v)
                MediaAuthorDetailActivity.start(
                    spiderName.context,
                    item.spider.userId
                )
            }
        }
        else {
            spiderAvatar.load(
                ContextCompat.getDrawable(
                    spiderAvatar.context,
                    R.drawable.ic_launcher
                )
            )
            spiderAttention.visibility = View.INVISIBLE
            split_line.visibility = View.INVISIBLE
            spiderName.text = spiderName.context.resources.getString(R.string.app_name)
            spiderName.setOnClickListener(null)
            spiderAvatar.setOnClickListener(null)
            spiderAttention.setOnClickListener(null)
        }*/

        val linkedHashSet = AppFactoryGlobalConfig.getAppServerConfigInfo(itemView.context)?.getNewsDetailConfig()?.getLinkedHashSet()
        linkedHashSet?.let {
            if (it.contains(NewsDetailStyleConfig.COMMENT_BUTTON)) {
                iv_comment.isClickable = true
                iv_comment.setOnClickListener {
                    ClickUtils.delayClickable(it)
                    if ("视频" == TXReportManager.getInstance().firstTabInfo.tabName) {
                        TXReportManager.getInstance().app_jhapp_video_interact("点击评论",item?.catalogName, item)
                    }
                    try {
                        NewsItemClickUtils.OpenItemNewsHandle(itemView.context, item.getType(), item, CatalogItem())
                    } catch (e: java.lang.Exception) {
                        e.printStackTrace()
                    }
                }
            }else{
                iv_comment.isClickable = false
            }
            if (it.contains(NewsDetailStyleConfig.LIKE_BUTTON)) {
                iv_collection.isClickable = true
                iv_collection.setOnClickListener {
                    ClickUtils.delayClickable(it)
                    if (UserInfo.isLogin(itemView.context)) {
                        if (SupportKit.isSupport(SupportKit.TYPE_1,item.id)) {
                            val like = AddLikeDataInvoke(object : DataInvokeCallBack<AddLikeDataInvoke.AddLikeDataReciver> {
                                override fun success(data: AddLikeDataInvoke.AddLikeDataReciver?) {
                                    data?.let {
                                        if (it.state) {
                                            iv_collection.isSelected = false
//                                            ToastUtil.show(itemView.context, "取消点赞");
                                        }
                                    }
                                }

                                override fun fault(data: Any?) {
                                }
                            }, itemView.context)
                            like.disableNotifyEvent = disableNotifyEvent
                            like.cancelNewsLike(item.id, UserInfo.getUserInfo(itemView.context).userid, 1,item)
                        } else {
                            val like =  AddLikeDataInvoke(object : DataInvokeCallBack<AddLikeDataInvoke.AddLikeDataReciver> {
                                override fun success(data: AddLikeDataInvoke.AddLikeDataReciver?) {
                                    data?.let {
                                        if (it.state) {
                                            iv_collection.isSelected = true
                                            //点赞
//                                            ToastUtil.show(itemView.context, "点赞成功")
                                        }
                                    }
                                }

                                override fun fault(data: Any?) {

                                }
                            }, itemView.context)
                            like.disableNotifyEvent = disableNotifyEvent
                            like.addNewsLike(itemView.context, item, UserInfo.getUserInfo(itemView.context).userid, 1)
                        }

                    } else {
                        LoginUtils.request_source = LoginUtils.source_like
                        LoginUtils.invokeLogin(itemView.context)
                    }
                }
            }else{
                iv_collection.isClickable = false
            }
            if (it.contains(NewsDetailStyleConfig.SHARE_BUTTON) || it.contains(NewsDetailStyleConfig.COLLECT_BUTTON)) {
                iv_share.isClickable = true
                iv_share.setOnClickListener {
                    ClickUtils.delayClickable(it)
                    shareWrap = ShareWrap(itemView.context, item.getArticleItem.type == TypeX.NEWS.MICRO_LIVE)
                    shareWrap?.setShowFunction(true)
                    shareWrap?.init(item.getArticleItem, null)
                    shareWrap?.show(itemView,true,true,true)

                }
            }else{
                iv_share.isClickable = false
            }

        }


        if (item != null) {
            val collectionController =
                CollectionController(
                    itemView.context,
                    null,
                    item,
                    object : CollectionController.CollectStatusListener {
                        override fun onCollectionStatus(sourceId: String, collected: Boolean) {}
                        override fun onCollectionOk(
                            sourceId: String,
                            collected: Boolean,
                            msg: String
                        ) {
                        }

                        override fun onCollectionErr(msg: String) {}
                    })
            collectionController.checkCollection()
        }

        SupportKit.initSupportViewState(itemView.context, SupportKit.TYPE_1, "${item.id}", iv_collection)




    }


    override fun getLogLayout(): View {
        return logoLayout
    }

    override fun getContainer(): FrameLayout {
        return logoContainer
    }

    override fun getPlayActionView(): View? {
        return videoTag
    }

    override fun getArticle(): ArticleItem? {
        return item
    }

    override fun destroy() {

    }
}